package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.AdPublishAppAccountCPARdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.PstAssign;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.AdPublishAppAccountCPA;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created by Jacky on 2015/1/31.
 */
@Service
public class AdPublishAppAccountCPARdbServiceImpl extends DaoTemplate implements
		AdPublishAppAccountCPARdbService {

	@Override
	public AdPublishAppAccountCPA getCPA(Long connectionObjectId,
			Long productConfigurationId, Long fbAccountId) {
		String sql = "SELECT * FROM ad_publish_appaccount_cpa " + "WHERE "
				+ "connection_object_id=? " + "AND product_configuration_id=? "
				+ "AND fb_account_id=? ";
		return findOne(sql, new Object[] { connectionObjectId,
				productConfigurationId, fbAccountId }, DefaultRowMapper.autoRowMapper(AdPublishAppAccountCPA.class));
	}


	@Override
	public void updateCPA(AdPublishAppAccountCPA cpa) {
		String sql = "UPDATE ad_publish_appaccount_cpa SET is_support_cpa=? WHERE 1=1 AND connection_object_id=? AND product_configuration_id=? AND fb_account_id=? ";
		Object[] param = new Object[] { cpa.getIsSupportCPA(),
				cpa.getConnectionObjectId(), cpa.getProductConfigurationId(),
				cpa.getFbAccountId() };
		getJdbcTemplate().update(sql, param);
	}


	/**
	 * @discription
	 * @author giser.yang 创建时间 2015年5月29日 下午5:35:01
	 * @param cpa
	 * @see com.ndp.fb.rdb.api.AdPublishAppAccountCPARdbService#saveCPANoUser(com.ndp.fb.rdb.model.AdPublishAppAccountCPA)
	 */

	@Override
	public void saveCPANoUser(final AdPublishAppAccountCPA cpa) {
		String sql = "INSERT INTO ad_publish_appaccount_cpa (connection_object_id, product_configuration_id,"
				+ " fb_account_id,is_support_cpa) VALUES (?,?,?,?)";
		PstAssign pst = new PstAssign() {
			@Override
			public void setParam(PreparedStatement ps) throws SQLException {
				ps.setObject(1, cpa.getConnectionObjectId());
				ps.setObject(2, cpa.getProductConfigurationId());
				ps.setObject(3, cpa.getFbAccountId());
				ps.setObject(4, cpa.getIsSupportCPA());
			}
		};
		save(sql, pst);

	}

	/**
	 * @discription
	 * @author giser.yang 创建时间 2015年5月29日 下午5:40:13
	 * @param cpa
	 * @see com.ndp.fb.rdb.api.AdPublishAppAccountCPARdbService#updateCPANoUser(com.ndp.fb.rdb.model.AdPublishAppAccountCPA)
	 */

	@Override
	public void saveOrUpdateCPANoUser(AdPublishAppAccountCPA cpa) {
		AdPublishAppAccountCPA cpa1 = this.getCPA(cpa.getConnectionObjectId(),
				cpa.getProductConfigurationId(), cpa.getFbAccountId());
		if (cpa1 == null) {
			this.saveCPANoUser(cpa);
		} else {
			this.updateCPA(cpa);
		}
	}

	/**
	 * @discription
	 * @author giser.yang 创建时间 2015年5月29日 下午6:16:41
	 * @param cpa
	 * @see com.ndp.fb.rdb.api.AdPublishAppAccountCPARdbService#updateCPANoUser(com.ndp.fb.rdb.model.AdPublishAppAccountCPA)
	 */

	@Override
	public void updateCPANoUser(AdPublishAppAccountCPA cpa) {
		String sql = "UPDATE ad_publish_appaccount_cpa SET is_support_cpa=? WHERE 1=1 AND connection_object_id=? AND product_configuration_id=? AND fb_account_id=? ";
		Object[] param = new Object[] { cpa.getIsSupportCPA(), cpa.getConnectionObjectId(), cpa.getProductConfigurationId(),
				cpa.getFbAccountId() };
		getJdbcTemplate().update(sql, param);

	}
}
